﻿//========================================
// All rights reserved (C) 2017-2100
// CLR 版本：	4.0.30319.42000
// 命名空间:	SFJDManagement
// 文 件 名：	HtmlGenerator
// 作    者：	WooG
// 联系方式：	wooglle@163.com
// 创建时间：	2017/10/28 22:05:20
//========================================
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;

namespace SFJDManagement
{
    public class HtmlGenerator
    {
        public string html;
        private StringBuilder strHTMLBuilder = new StringBuilder();

        public HtmlGenerator(DataGrid dataGrid)
        {
            Ini();
            GenerateByDataGrid(dataGrid);
        }

        private void Ini()
        {
            strHTMLBuilder.Append("<html>");
            strHTMLBuilder.Append("<head>");
            strHTMLBuilder.Append("<meta charset='utf-8'>");
            strHTMLBuilder.Append("<title>打印预览</title>");
            strHTMLBuilder.Append("<style type='text/css'>");
            strHTMLBuilder.Append("h1,h2,h3{text-align:center;}");
            strHTMLBuilder.Append("table,table tr th,table tr td{border:1px solid;}");
            strHTMLBuilder.Append("table thead{display:table-header-group;font-weight:bold}");
            strHTMLBuilder.Append("table{margin:0 auto; border-collapse:collapse; font-family:仿宋; font-size:smaller}");
            strHTMLBuilder.Append("</style>");
            strHTMLBuilder.Append("</head>");
            strHTMLBuilder.Append("<body>");
            strHTMLBuilder.Append("<h2>" + Runtime.institution +"</h2>");
        }

        private void GenerateByDataGrid(DataGrid dataGrid)
        {
            strHTMLBuilder.Append("<table>");
            strHTMLBuilder.Append("<thead><tr>");
            List<int> columnsVisible = new List<int>(10);

            for(int i = 0; i < dataGrid.Columns.Count; i++)
            {
                if(dataGrid.Columns[i].Visibility == System.Windows.Visibility.Visible)
                {
                    columnsVisible.Add(i);
                    strHTMLBuilder.Append("<th>");
                    strHTMLBuilder.Append(dataGrid.Columns[i].Header);
                    strHTMLBuilder.Append("</th>");
                }
            }
            strHTMLBuilder.Append("</tr></thead>");
            strHTMLBuilder.Append("<tbody>");
            strHTMLBuilder.Append("<tfoot>");
            strHTMLBuilder.Append("</tfoot>");
            string temp = "";
            for (int i = 0; i < dataGrid.Items.Count; i++)
            {
                DataGridRow gridRow = dataGrid.GetRow(i);
                if(gridRow.Visibility == System.Windows.Visibility.Visible)
                {
                    strHTMLBuilder.Append("<tr>");
                    for (int j = 0; j < columnsVisible.Count; j++)
                    {
                        strHTMLBuilder.Append("<td>");
                        temp = (dataGrid.GetCell(i, columnsVisible[j]).Content as TextBlock).Text;
                        strHTMLBuilder.Append(temp.Replace("\r\n", "<br>"));
                        strHTMLBuilder.Append("</td>");
                    }
                    strHTMLBuilder.Append("</tr>");
                }
            }
            strHTMLBuilder.Append("</tbody>");
            strHTMLBuilder.Append("</table>");
            strHTMLBuilder.Append("</body>");
            strHTMLBuilder.Append("</html>");
        }

        public string GetHtml()
        {
            return strHTMLBuilder.ToString();
        }
    }
}
